/*
 * Created on Aug 22, 2004
 */
package org.vectrics.email.testhtml;

import java.util.Properties;

import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vectrics.common.util.LocalMachineUtils;
import org.vectrics.domain.Domain;
import org.vectrics.mail.MailException;
import org.vectrics.mail.MailMessage;
import org.vectrics.mail.UserMailer;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.user.Person;
import org.vectrics.user.User;

/**
 * @author mmoore
 */
public class HtmlEmailTestAction extends Action {
    private transient final Logger log = Logger.getLogger(this.getClass());

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws JspException {

        sendMessage(CoreServiceLocator.getConfiguration().getSystemDomain());
        return (mapping.findForward("success"));
    }

    public void sendMessage(Domain domain) {
        log.info("Sending html e-mail");
        Person person = CoreServiceLocator.getPersonService().findPersonWithUserName(domain, "mmoore");
        if (person == null) {
            person = new Person();
            person.setLastName("Moore");
            person.setFirstName("Mike");
            person.createContactInfoIfNotExist().setEmail("mmoore@vectrics.com");
        }
        User user = person.createUserIfNotExist();
        user.setUserName("mmoore");
        user.setPassword("mmoore");
        user.setPasswordEncrypted(false);
        CoreServiceLocator.getPersonService().updatePerson(person);

        UserMailer mailer = new UserMailer(CoreServiceLocator.getConfiguration().getFromMailConfiguration(domain));
        try {
            MailMessage mailMessage = new MailMessage(CoreServiceLocator.getSystemService().findSystemDomain());
            mailMessage.setBody(getHtmlBody());
            mailMessage.setFromAddress("mmoore@vectrics.com");
            mailMessage.setFromName("Vectrics Test");
            String hostName = LocalMachineUtils.findHostName() + "  /  " + LocalMachineUtils.findHostAddress();

            mailMessage.setSubject("HTML Test " + hostName);
            mailMessage.setContentType(MailMessage.CONTENT_HTML);
            mailer.sendMessageAsynch(person, mailMessage);
        } catch (MailException me) {
            log.error("MailException sending test message", me);

        }
        log.info("E-mailing finished");
    }

    protected String getHtmlBody() {
        StringBuffer html = new StringBuffer();
        html.append("<html>");
        // html.append("  <head>");
        // html.append(
        // "     <meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
        // );
        // html.append("     <title>Reports</title>");
        //html.append("     <meta http-equiv=\"pragma\" content=\"no-cache\">");
        // html.append(
        // "     <link href=\"http://www.vectrics.com/hrs-intranet/custom/main.css\" type=\"text/css\" rel=\"stylesheet\">"
        // );
        // html.append("  </head>");
        html.append("  <body>");
        html.append(" 		<h2>Hello World</h2>");
        html.append(" 		   <table width=\"500\">");
        html
                .append(" 		      <tr><td bgcolor=\"#aa66aa\"><font size=\"8\" style=\"font:normal 1em Verdana\">Mike Moore</font></td><td><font>303-978-1915</font></td></tr>");
        html
                .append(" 		      <tr><td bgcolor=\"#aa66aa\"><font>Mike Moore</font></td><td><font>303-978-1915</font></td></tr>");
        html
                .append(" 		      <tr><td bgcolor=\"#aa66aa\"><font>Mike Moore</font></td><td><font>303-978-1915</font></td></tr>");
        html
                .append(" 		      <tr><td bgcolor=\"#aa66aa\"><font>Mike Moore</font></td><td><font>303-978-1915</font></td></tr>");
        html.append(" 		   </table>");
        html.append("  </body>");
        html.append("</html>");
        return (html.toString());
    }

    protected Session getMailSession() {
        Session session = null;
        Properties properties = new Properties();

        properties.put("mail.host", RecruitConfig.getRecruitInstance().getMailHost());
        properties.put("mail.password", RecruitConfig.getRecruitInstance().getMailPassword());
        properties.put("mail.smtp.host", RecruitConfig.getRecruitInstance().getMailHost());
        properties.put("mail.smtp.password", RecruitConfig.getRecruitInstance().getMailPassword());

        session = Session.getInstance(properties);

        return (session);
    }

}
